<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/7/7
 * Time: 17:31
 */

namespace Moblie\Controller;

use Org\Util\Wechat;
use Face\Controller\CommonController;

class WechatController extends CommonController
{
    /*配对微信*/
    public function check()
    {
        $options = [
            'token'=>'fanjun',
            'appid'=>'wxe073077e395869ec',
            'appsecret'=>'b034af05c5bebcdc84c4380f6a63b579',
        ];

        $weObj = new Wechat($options);
        $weObj->valid();
        //获取菜单操作
        $menu  = $weObj->getMenu();
        //设置菜单
        $newmenu = [
            "button"=>[
                ['type'=>'view','name'=>'代购Web','url'=>'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Moblie&m=Index&a=index'],
                [
                    'name'=>'我的发布',
                    'sub_button'=>[
                        [
                            'name'=>'二维码分享',
                            'type'=>'view',
                            'url'=>'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Moblie&m=Wechat&a=two_code'
                        ],
                        [
                            'name'=>'显示中的信息',
                            'type'=>'view',
                            'url'=>'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Moblie&m=Wechat&a=wechatcode&type=3'
                        ],
                        [
                            'name'=>'审核中的信息',
                            'type'=>'view',
                            'url'=>'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Moblie&m=Wechat&a=wechatcode&type=2'
                        ],
                        [
                            'name'=>'免费发布信息',
                            'type'=>'view',
                            'url'=>'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Moblie&m=Wechat&a=wechatcode&type=1'
                        ],
                    ]
                ],
            ]
        ];
        $result = $weObj->createMenu($newmenu);

        $type = $weObj->getRev()->getRevType();
        switch($type) {
            case Wechat::MSGTYPE_TEXT:
                $weObj->text('哦！')->reply();
                exit;
                break;
            case Wechat::MSGTYPE_EVENT:
                $weObj->text('欢迎你关注我！')->reply();
                exit;
                break;
            case Wechat::MSGTYPE_IMAGE:
                $weObj->text("看到了！")->reply();
                break;
            default:
                $weObj->text("help info")->reply();
        }
    }
    /*以下是微信登陆*/
    /*type=1发布信息
       type=2审核中信息
        type=3显示中的信息
    */
    /*获取微信code*/
    public function wechatcode()
    {
        $type = i('get.type');
        $reurl = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Moblie&m=Wechat&a=wechatuser&type='.$type;
        $redirecturl= urlencode($reurl);
        $appid='wxe073077e395869ec';
        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        header("Location:$url");
    }
    /*获取微信用户信息*/
    public function wechatuser()
    {
        /*得到type*/
        $type = i('get.type');
        /*得到code*/
        $code= $_GET['code'];
        $appid= 'wxe073077e395869ec';
        $appsecret='b034af05c5bebcdc84c4380f6a63b579';

        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";

        $jsoninfo = $this->https($url);
        /*获取到access_token*/
        $access_token=$jsoninfo['access_token'];
        
        /*得到用户的openid*/
        $openid = $jsoninfo['openid'];

        $urll = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
        /*得到用户的信息*/
        $jsoninfo = $this->https($urll);

        $nickname=$jsoninfo['nickname'];
        $openid = $jsoninfo['openid'];
        $headimg = $jsoninfo['headimgurl'];

        $_SESSION['wechatopenid']=$openid;
        $_SESSION['wechatimg']=$headimg;
        $_SESSION['wechatname']=$nickname;

        $data = [
            'wechatname'=>$nickname,
            'openid'=>$openid,
            'wechatimg'=>$headimg,
        ];
        $save = [
            'wechatname'=>$nickname,
            'openid'=>$openid,
            'wechatimg'=>$headimg,
        ];
        if(M('wechatuser')->where(['openid'=>$openid])->find()){
            M('wechatuser')->where(['openid'=>$openid])->save($save);
            if($type==1){
                $this->redirect('Moblie/Send/index');
            }elseif ($type==2){
                $this->redirect('Moblie/Send/check');
            }else{
                $this->redirect('Moblie/Send/pass');
            }
        }else{
            M('wechatuser')->add($data);
            if($type==1){
                $this->redirect('Moblie/Send/index');
            }elseif ($type==2){
                $this->redirect('Moblie/Send/check');
            }else{
                $this->redirect('Moblie/Send/pass');
            }
        }
    }
    /*获取jsapi_ticket*/
    public function ticket()
    {
        $access_token = $this->access_token();

        $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$access_token.'&type=jsapi';
        $data = $this->https($url);
        $_SESSION['ticket'] = $data['ticket'];
        /*随机字符串*/
        $noncestr='Wm3WZYTPz0wzccnW';
        /*时间戳*/
        $time = time();
        $reurl = 'http://'.$_SERVER['HTTP_HOST'].'/index.php?g=Moblie&m=Index&a=index';
        $jsapi_ticket=$_SESSION['ticket'].'&noncestr='.$noncestr.'&timestamp='.$time.'&url='.$reurl;
        /*得到signature*/
        $signature =  sha1($jsapi_ticket);
        $_SESSION['signature'] = $signature;
        $_SESSION['sitime'] = $time;
    }
    /*获取access_token*/
    public function access_token()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxe073077e395869ec&secret=b034af05c5bebcdc84c4380f6a63b579';
        $data = $this->https($url);
        return $access_token = $data['access_token'];
    }
    /*永久二维码*/
    public function two_code()
    {
        //永久二维码
        $qrcode = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": 123}}}';
        $access_token = $this->access_token();

        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;

        $tick = $this->https($url,$qrcode);

        $ticket  = $tick['ticket'];

        $ticketuc = urlencode($ticket);

        $erwei = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$ticketuc;

        $this->assign('erwei',$erwei);
        $this->display();
    }
    /*curl*/
    public function https($url,$data=NULL)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
         if (!empty($data)){
             curl_setopt($curl, CURLOPT_POST, 1);
             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);

        $jsonin=json_decode($output,true);
        return $jsonin;
    }
}